import { getRequest } from "@/api/request";
import { AxiosResponse } from "axios";
const REQUEST_URL = {
  search: "/search", //普通搜索
  searchAll: "/search/multimatch", // 多重匹配搜索
  hotSearch: "/search/hot/detail", // 热搜榜
  serarchDetail: "/cloudsearch",
};

export interface HotItem {
  searchWord: string;
}

// interface SearchList {
//   name: string;
//   picUrl: string;
//   id: number | string;
//   songName: string;
// }

const filter = (res: AxiosResponse) => {
  return (
    (res?.data?.result?.songs || []).map(
      (it: {
        name: string;
        artists: { img1v1Url: string; name: string }[];
        id: number;
      }) => {
        return {
          name: it.name,
          picUrl: it.artists[0].img1v1Url,
          id: it.id,
          songName: it.artists[0].name,
        };
      }
    ) || []
  );
};

export interface AritisFace {
  occupation: string;
  name: string;
  id: string;
  img1v1Url: string;
  fansSize: string;
  musicSize: string;
}

export interface PramsFace {
  type: string;
  keywords: string;
  limit: number;
  offset: number;
}

export interface SongSheetFace {
  coverImgUrl: string;
  name: string;
  trackCount: string;
  creator: { nickname: string };
  id: string;
  playCount: string;
}

export interface VideoFace {
  coverUrl: string;
  title: string;
  creator: { userName: string }[];
  playTime: string;
}

const getSearchAllFilter = (res: AxiosResponse) => {
  if (res.data.result.artist) {
    const artist = res.data.result.artist.map((it: AritisFace) => {
      return {
        occupation: it.occupation,
        name: it.name,
        id: it.id,
        img1v1Url: it.img1v1Url,
        fansSize: it.fansSize,
        musicSize: it.musicSize,
      };
    });
    return artist;
  } else {
    return [];
  }
};

const handleGetHotSearch = (res: AxiosResponse) => {
  if (res.data.data) {
    const HotList = res.data.data.map((it: HotItem) => {
      return {
        searchWord: it.searchWord,
      };
    });
    return HotList;
  } else {
    return [];
  }
};

const getGetSerarchDetail = (type: string, res: AxiosResponse) => {
  console.log(res);
  console.log(type);
  switch (type) {
    case "1":
      if (res.data.result?.songs) {
        // 单曲
        const list = res.data.result.songs.map(
          (it: {
            name: string;
            al: { picUrl: string };
            id: string;
            ar: { name: string }[];
          }) => {
            return {
              name: it.name,
              picUrl: it.al.picUrl,
              id: it.id,
              songName: it.ar[0].name,
            };
          }
        );
        return list;
      } else {
        return [];
      }
      break;
    case "1000":
      if (res.data.result.playlists) {
        const total = res.data.result.playlistCount;
        const list = res.data.result.playlists.map((it: SongSheetFace) => {
          return {
            coverImgUrl: it.coverImgUrl,
            name: it.name,
            trackCount: it.trackCount,
            nickname: it.creator.nickname,
            id: it.id,
            playCount: it.playCount,
          };
        });
        return {
          total: total,
          list: list,
        };
      } else {
        return {
          total: 0,
          list: [],
        };
      }
      break;
    case "1014":
      if (res.data.result.videos) {
        const total = res.data.result.videoCount;
        const list = res.data.result.videos.map((it: VideoFace) => {
          return {
            coverUrl: it.coverUrl,
            title: it.title,
            userName: it.creator[0].userName,
            playTime: it.playTime,
          };
        });

        return {
          total: total,
          list: list,
        };
      } else {
        return {
          total: 0,
          list: [],
        };
      }
      break;
    case "100":
      if (res.data.result.artists) {
        const total = res.data.result.artistCount;
        const list = res.data.result.artists.map((it: AritisFace) => {
          return {
            occupation: it.occupation || "用户名",
            name: it.name,
            id: it.id,
            img1v1Url: it.img1v1Url,
            fansSize: it.fansSize || 0,
            musicSize: it.musicSize || 0,
          };
        });

        return {
          total: total,
          list: list,
        };
      } else {
        return {
          total: 0,
          list: [],
        };
      }
      break;
  }
};

export const getSearch = (keywords: number | string | undefined) => {
  return getRequest(REQUEST_URL.search + `?keywords=${keywords}`).then(filter);
};

export const getSearchAll = (keywords: number | string | undefined) => {
  return getRequest(REQUEST_URL.searchAll + `?keywords=${keywords}`).then(
    getSearchAllFilter
  );
};

export const getSerarchDetail = (prams: PramsFace) => {
  return getRequest(
    REQUEST_URL.serarchDetail +
      `?keywords=${prams.keywords}&type=${prams.type}&limit=${prams.limit}&offset=${prams.offset}`
  ).then((res) => getGetSerarchDetail(prams.type, res));
};

export const getHotSearch = () => {
  return getRequest(REQUEST_URL.hotSearch).then(handleGetHotSearch);
};
